import { createRouter, createWebHistory, RouteRecordRaw } from "vue-router";
import Layout from "../views/Layout.vue";

const routes: Array<RouteRecordRaw> = [
  {
    //主页
    path: "/",
    name: "Layout",
    component: Layout,
    redirect: "/home",
    meta: {
      auth: true,
    },
    children: [
      //子路由
      {
        path: "home",
        name: "Home",
        component: () =>
          import(/* webpackChunkName: "Home" */ "../views/home/home.vue"),
        meta: {
          auth: true,
          Breaddata: null,
        },
      },
      {
        path: "userlist",
        name: "Userlist",
        component: () =>
          import(
            /* webpackChunkName: "userlist" */ "../views/user/userList.vue"
          ),
        meta: {
          auth: true,
          Breaddata: {
            parents: "用户管理",
            current: "用户列表",
          },
        },
      },
      {
        path: "rolelist",
        name: "Rolelist",
        component: () =>
          import(
            /* webpackChunkName: "rolelist" */ "../views/auth/roleList.vue"
          ),
        meta: {
          auth: true,
          Breaddata: {
            parents: "权限管理",
            current: "角色列表",
          },
        },
      },
      {
        path: "authlist",
        name: "Authlist",
        component: () =>
          import(
            /* webpackChunkName: "Authlist" */ "../views/auth/authList.vue"
          ),
        meta: {
          auth: true,
          Breaddata: {
            parents: "权限管理",
            current: "权限列表",
          },
        },
      },
      {
        path: "goodslist",
        name: "Goodlist",
        component: () =>
          import(
            /* webpackChunkName: "Authlist" */ "../views/goods/goodsList.vue"
          ),
        meta: {
          auth: true,
          Breaddata: {
            parents: "商品管理",
            current: "商品列表",
          },
        },
      },
      {
        path: "classparams",
        name: "Classparams",
        component: () =>
          import(
            /* webpackChunkName: "Classparams" */ "../views/goods/classParams.vue"
          ),
        meta: {
          auth: true,
          Breaddata: {
            parents: "商品管理",
            current: "分类参数",
          },
        },
      },
      {
        path: "goodsclass",
        name: "Goodsclass",
        component: () =>
          import(
            /* webpackChunkName: "Goodsclass" */ "../views/goods/goodsClass.vue"
          ),
        meta: {
          auth: true,
          Breaddata: {
            parents: "商品管理",
            current: "商品分类",
          },
        },
      },
      {
        path: "orderlist",
        name: "Orderlist",
        component: () =>
          import(
            /* webpackChunkName: "Orderlist" */ "../views/order/orderList.vue"
          ),
        meta: {
          auth: true,
          Breaddata: {
            parents: "订单管理",
            current: "订单列表",
          },
        },
      },
      {
        path: "datareport",
        name: "Datareport",
        component: () =>
          import(
            /* webpackChunkName: "Datareport" */ "../views/stats/dataReport.vue"
          ),
        meta: {
          auth: true,
          Breaddata: {
            parents: "数据统计",
            current: "数据报表",
          },
        },
      },
    ],
  },
  {
    path: "/Login",
    name: "Login",
    meta: {
      auth: false,
    },
    component: () =>
      import(/* webpackChunkName: "Login" */ "../views/Login.vue"),
  },
];

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
});
router.beforeEach((to, from, next) => {
  console.log(to.meta.auth);
  if (to.meta.auth) {
    //路由元信息里的需登录验证为true
    const token = localStorage.getItem("Stytoken") || undefined;
    if (token) next();
    else next(`/Login?redirect=${to.path}`);
  } else {
    next();
  }
});
export default router;
